package floappcontest.core.backbeans;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import floappcontest.core.beans.Tutor;

/**
 * When a professor wants to join FLOApp Contest his data
 * must be sent to FLOApp Contest admin via e-mail.
 * 
 * Mail sender is responsible to send this data via e-mail to
 * FLOApp Contest Administrator.
 * 
 * The file located at ./files/maildata should have the following structure:
 * 1st line: mail.smtp.host
 * 2nd line: my-mail-server
 * 3rd line: me@example.com
 * 
 * @author elloa, ricardo, odilon
 *
 */
public class MailSender {

	private static final String END_OF_LINE = System.getProperty( "line.separator" ); //$NON-NLS-1$
	private static final String E_MAIL = "E-mail: "; //$NON-NLS-1$
	private static final String IES = "IES: "; //$NON-NLS-1$
	private static final String NAME = "Name: "; //$NON-NLS-1$
	private static final String PASSWORD = "Password "; //$NON-NLS-1$
	private static final String LOGIN = "Login: "; //$NON-NLS-1$
	private static final String DEFAULT_SUBJECT = "Cadastro Tutor"; //$NON-NLS-1$
	private static final String FILES_MAIL_DATA = "./files/mailData"; //$NON-NLS-1$
	private static final String FAILED = "FAILED"; //$NON-NLS-1$
	private static final String OK = "OK"; //$NON-NLS-1$
	
	private Properties p;
	private String host;
	private String mailServer;
	private String receiver;
	private Tutor tutor;

	/**
	 * Initializes a MailSender setting the informations from the file maildata.
	 */
	public MailSender(){

		try{
			BufferedReader br = new BufferedReader(new FileReader(FILES_MAIL_DATA));

			setHost(br.readLine());
			setMailServer(br.readLine());
			setReceiver(br.readLine());

			setP( new Properties() );
			getP().put( getHost(),getMailServer());

		} catch (FileNotFoundException e0) {
			e0.printStackTrace();
		} catch (IOException e1){
			e1.printStackTrace();
		}

	}

	/**
	 * Set the host value from a new given host.
	 * 
	 * @param host the new host
	 */
	public void setHost(String host) {
		this.host = host;
	}

	/**
	 * Set the mailServer value from a new given mailServer.
	 * 
	 * @param mailServer the new value of mailServer
	 */
	public void setMailServer(String mailServer) {
		this.mailServer = mailServer;
	}

	/**
	 * Set the receiver from a new given receiver
	 * 
	 * @param receiver the new value for receiver
	 */
	public void setReceiver(String receiver) {
		this.receiver = receiver;
	}
	
	
	/**
	 * Sends the e-mail to Floapp Admin with the tutor's data as 
	 * message's contents.
	 * 
	 * @return "OK" if the e-mail was sent successfully and "FAILED" if wasn't.
	 */
	public String sendMail(){

		Session session = Session.getInstance(getP(), null);
		MimeMessage msg = new MimeMessage(session);

		try{

			msg.setFrom(new InternetAddress(getReceiver()));
			msg.setRecipient(Message.RecipientType.TO, new InternetAddress(getReceiver()));
			msg.setSentDate(new Date());

			msg.setSubject( DEFAULT_SUBJECT );
			
			msg.setText(LOGIN + this.tutor.getLogin() + END_OF_LINE + 
					    PASSWORD + this.tutor.getPassword() + END_OF_LINE + 
					    NAME + this.tutor.getName() + END_OF_LINE + 
					    IES + this.tutor.getIes() + END_OF_LINE+
					    E_MAIL + this.tutor.getEmail()+ END_OF_LINE
			);

			Transport.send(msg);


		} catch (MessagingException e0){
			return FAILED;
		}
		return OK;

	}
	
	/**
	 * Returns the name of the tutor who wants to join floapp
	 * 
	 * @return the tutor's name 
	 */
	public String getTutorName(){
		return this.tutor.getName();
	}
	
	/**
	 * Returns the email of the tutor who wants to join floapp contest.
	 * 
	 * @return new tutor's email
	 */
	public String getTutorEmail(){
		return this.tutor.getEmail();
	}
	
	/**
	 * Returns the Teaching Instution of the tutor who wants to join floapp contest.
	 * 
	 * @return the new Tutor's Teaching Institution Name
	 */
	public String getTutorIes(){
		return this.tutor.getIes().getName();
	}
	
	/**
	 * Modifies new tutor's name
	 * 
	 * @param name tutor's new name
	 */
	public void setTutorName(String name){
		this.tutor.setName(name);
	}
	
	/**
	 * Set new Tutor's e-mail address
	 * 
	 * @param mail the new tutor's email value
	 */
	public void setTutorMail(String mail){
		this.tutor.setEmail(mail);
	}
	
	/**
	 * Modifies new tutor's teaching Institution
	 * 
	 * @param ies the new teaching institution name
	 */
	public void setTutorIes(String ies){
//		this.tutor.setIes(ies);
	}
	
	/**
	 * Modifies new tutor login
	 * 
	 * @param login the new login
	 */
	public void setTutorLogin(String login){
		this.tutor.setLogin(login);
	}
	
	/**
	 * Gets the login of the new tutor
	 * 
	 * @return the login at Floapp Contest
	 */
	public String getTutorLogin(){
		return this.tutor.getLogin();
	}
	
	/**
	 * Modifies the new Tutor's password
	 * 
	 * @param pass the new Tutor's password
	 */
	public void setTutorPassword(String pass){
		this.tutor.setPassword(pass);
	}
	
	/**
	 * Returns the new Tutor's password
	 * 
	 * @return the new tutor's password
	 */
	public String getTutorPassword(){
		return this.tutor.getPassword();
	}

	
	public Properties getP() {
		return this.p;
	}

	
	public void setP( Properties p ) {
		this.p = p;
	}

	
	public String getHost() {
		return this.host;
	}

	
	public String getMailServer() {
		return this.mailServer;
	}

	
	public String getReceiver() {
		return this.receiver;
	}
}